﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace ContentTypeCustomForms2.CONTROLTEMPLATES.MyFolder
{
    public partial class MyUserControl2 : UserControl
    {
        class Country
        {
            public string Name;
            public List<string> Towns;
        }
        List<Country> Countries = new List<Country> {
            new Country { Name = "Denmark", Towns = new List<string> { "Copenhagen", "Århus", "Aalborg", "Roskilde", "Odense"}},
            new Country { Name = "Finland", Towns = new List<string> { "Helsinki", "Espoo", "Tampere", "Vantaa", "Turku"}},
            new Country{ Name="Norway", Towns = new List<string> { "Oslo", "Bergen", "Trondheim", "Stavanger", "Fredrikstad/Sarpsborg" }},
            new Country{ Name="Sweden", Towns = new List<string> { "Stockholm", "Gothenburg", "Malmö", "Uppsala", "Västerås"}} };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit) LoadFieldsValues();
                else
                {
                    string[] s = { string.Empty };
                    ddlCountries.DataSource = s.Concat(from x in Countries
                                              select x.Name);
                    ddlCountries.DataBind();
                }
            }
            else
            {
                SPUtility.ValidateFormDigest();
                SaveFieldsValues();
            }
        }
        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (null != ddlCountries.SelectedItem)
            {
                string[] s = {string.Empty};
                ddlTowns.DataSource = s.Concat(from x in Countries
                                      where x.Name == ddlCountries.SelectedItem.Text
                                      from y in x.Towns
                                      select y);
                ddlTowns.DataBind();
            }
        }
        private void SaveFieldsValues()
        {
            foreach (object field in SPContext.Current.FormContext.FieldControlCollection)
            {
                TextField tField = field as TextField;
                if (null != tField && "Country" == tField.FieldName && null != ddlCountries.SelectedItem)
                {
                    tField.Value = ddlCountries.SelectedItem.Text;
                    tField.ItemFieldValue = ddlCountries.SelectedItem.Text;
                }
                if (null != tField && "Town" == tField.FieldName && null != ddlTowns.SelectedItem)
                {
                    tField.Value = ddlTowns.SelectedItem.Text;
                    tField.ItemFieldValue = ddlTowns.SelectedItem.Text;
                }
            }
        }
        private void LoadFieldsValues()
        {
            string[] s = { string.Empty };
            ddlCountries.DataSource = s.Concat(from x in Countries
                                               select x.Name);
            ddlCountries.DataBind();
            string country = (string)SPContext.Current.ListItem["Country"];
            ListItem li = ddlCountries.Items.FindByText(country);
            if (null != li)
            {
                ddlCountries.ClearSelection();
                li.Selected = true;
                ddlTowns.DataSource = s.Concat(from x in Countries
                                               where x.Name == ddlCountries.SelectedItem.Text
                                               from y in x.Towns
                                               select y);
                ddlTowns.DataBind();
                string town = (string)SPContext.Current.ListItem["Town"];
                li = ddlTowns.Items.FindByText(town);
                if (null != li)
                {
                    ddlTowns.ClearSelection();
                    li.Selected = true;
                }
            }
        }
    }
}